package com.study.controller;

import com.study.common.PageResult;
import com.study.common.Result;
import com.study.common.ResultCode;
import com.study.entity.system.Role;
import com.study.entity.system.RoleAndUserRelations;
import com.study.service.RoleService;
import com.study.service.UserAndRoleRelationsService;
import com.study.vo.system.RoleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wfw
 * @Date 2020/07/17 11:04
 */
@CrossOrigin
@RestController
@RequestMapping("sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserAndRoleRelationsService userAndRoleRelationsService;

    /**
     * 分配权限
     */
    @PutMapping("assignPrem")
    public Result save(@RequestBody Map<String,Object> map){

        // 获取被分配的角色id
        String roleId = (String) map.get("id");
        // 获取到权限的id列表
        List<String> permIds = (List<String>) map.get("permIds");
        // 调用service完成权限分配
        roleService.assignPerms(roleId , permIds);

        return new Result(ResultCode.SUCCESS);
    }

    @PostMapping("")
    public Result add(@RequestBody Role role) {
        role.setCompanyId(companyId);
        roleService.save(role);
        return Result.SUCCESS();
    }

    @PutMapping("{id}")
    public Result update(@PathVariable(name = "id") String id, @RequestBody Role role) {
        roleService.update(role);
        return Result.SUCCESS();
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable(name = "id") String id) {
        roleService.delete(id);
        return Result.SUCCESS();
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable(name = "id") String id) {
        Role role = roleService.findById(id);
        RoleResult roleResult = new RoleResult(role);
        return new Result(ResultCode.SUCCESS,roleResult);
    }

    @GetMapping("")
    public Result findByPage(int page,int pagesize) {
        Page<Role> searchPage = roleService.findByPage(companyId, page, pagesize);
        PageResult<Role> pr = new PageResult(searchPage.getTotalElements(),searchPage.getContent());
        return new Result(ResultCode.SUCCESS,pr);
    }

    @GetMapping("list")
    public Result findAll() {
        List<Role> roleList = roleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS,roleList);
    }

    /**
     * 根据用户id获取全部权限
     * @param id
     * @return
     */
    @GetMapping("userId/{id}")
    public Result findRolesByUserId(@PathVariable(name = "id") String id) {
        List<RoleAndUserRelations> roleByUserId = userAndRoleRelationsService.findRoleByUserId(id);
        if (!ObjectUtils.isEmpty(roleByUserId)){
            List<String> roleIds = new ArrayList<>();
            roleByUserId.forEach(roleAndUserRelations -> roleIds.add(roleAndUserRelations.getId()));
            List<Role> roles = userAndRoleRelationsService.getRoleDetailByRoleId(roleIds);
            if (!ObjectUtils.isEmpty(roles)){
                return new Result(ResultCode.SUCCESS , roles);
            }
        }
        return new Result(ResultCode.SUCCESS);
    }
}
